package com.qst.binzhou2022.daos;

import com.qst.binzhou2022.beans.UserInfo;
import com.qst.binzhou2022.utils.Db;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;


public class UserInfoDao  implements  UserInfoDaoInter{
	
/*	public  List<UserInfo> getUserInfo(UserInfo userInfo) throws SQLException {
		boolean flag = false;
	  Connection connection =  Db.getConnection();
	  //statement  callableStatement
	  StringBuffer sb = new StringBuffer();
	  sb.append(" Select * from user_info where 1=1 ");
	  if(userInfo.getUserName()!=null &&!"".equals(userInfo.getUserName())){
		  sb.append(" and user_name='"+ userInfo.getUserName() +"'") ;
	  }
	  if(userInfo.getUserSex()!=null &&!"".equals(userInfo.getUserSex())){
			  sb.append(" and user_sex='"+ userInfo.getUserSex() +"'") ;
	  }
	  if(userInfo.getUserAge()!=null &&!"".equals(userInfo.getUserAge())){
		  sb.append(" and user_age='"+ userInfo.getUserAge() +"'") ;
	  }
	  PreparedStatement  statement = connection.prepareStatement(sb.toString());
	  ResultSet result = statement.executeQuery();
	  List<UserInfo> userInfoList = new  ArrayList<UserInfo>();
	  while(result.next()) {
		  UserInfo userInfoTemp = new UserInfo();
		  userInfoTemp.setUserAge(result.getInt("user_age"));
		  userInfoTemp.setUserSex(result.getString("user_sex"));	
		  userInfoTemp.setUserName(result.getString("user_name"));	
		  userInfoList.add(userInfoTemp);
  	 }
	  return userInfoList;
	}*/

	public  List<UserInfo> getUserInfo(UserInfo userInfo) throws SQLException {
		boolean flag = false;
		List<UserInfo> userInfoList = new ArrayList<>();
		//使用Mybatis 进行数据查询返回业务需要的userInfoList
		String resource = "sqlMapConfig.xml";
		InputStream inputStream = null;
		try {
			inputStream = Resources.getResourceAsStream(resource);
		} catch (IOException e) {
			e.printStackTrace();
		}
		//建造者模式
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession sqlSession = sqlSessionFactory.openSession();
		//目标  执行SQL
		userInfo.setUserName("a");

		userInfoList = sqlSession.selectList("getUserInfoById",userInfo);

		//先写sql
/*		UserInfo userInfo1 = sqlSession.selectOne("getUserInfoById");
		userInfoList.add(userInfo1);*/
	/*	Map<Integer,UserInfo> map = sqlSession.selectMap("getUserInfoMap","userId");

		Set<Integer> userIds = map.keySet();
		for (Integer userId : userIds) {
			System.out.println(map.get(userId));
		}
*/
		userInfoList.forEach(System.out::println);
		return userInfoList;
	}


	public  int addUserInfo(UserInfo userInfo) throws SQLException {
		int addResult = 0;
		//使用Mybatis 进行数据查询返回业务需要的userInfoList
		String resource = "sqlMapConfig.xml";
		InputStream inputStream = null;
		try {
			inputStream = Resources.getResourceAsStream(resource);
		} catch (IOException e) {
			e.printStackTrace();
		}
		//建造者模式
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession sqlSession = sqlSessionFactory.openSession();
		//目标  执行SQL

		addResult = sqlSession.insert("addUserInfo",userInfo);

		System.out.println(userInfo);

		sqlSession.commit();

		return addResult;
	}



}
